﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using BasicData.Domain.Entities;
using BasicData.Infrastructure.Interfasce;
using BasicData.Infrasturctrue;
using Microsoft.EntityFrameworkCore;

namespace BasicData.Infrastructure.Emplment
{
    /// <summary>
    /// 通用仓储实现类
    /// </summary>
    /// <typeparam name="T">实体类型</typeparam>
    public class BaseRepositroy<T> : IBaseRepositroy<T> where T : class
    {
        protected readonly MyDbContext _dbContext;
        protected readonly DbSet<T> _dbSet;
        public MyDbContext Db
        {
            get { return _dbContext; }
        }

       

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbContext">数据库上下文</param>
        public BaseRepositroy(MyDbContext dbContext)
        {
            _dbContext = dbContext;
            _dbSet = dbContext.Set<T>();
        }

        #region 查询操作

        /// <summary>
        /// 根据ID获取实体
        /// </summary>
        /// <param name="id">实体ID</param>
        /// <returns>实体对象</returns>
        public IQueryable<T> GetByIdAsync(object id)
        {
            return _dbSet.Where(e => EF.Property<object>(e, "Id").Equals(id));
        }

        /// <summary>
        /// 获取所有实体
        /// </summary>
        /// <returns>实体集合</returns>
        public IQueryable<T> GetAllAsync()
        {
            return _dbSet.AsQueryable();
        }

        /// <summary>
        /// 获取符合条件的第一个实体
        /// </summary>
        /// <param name="predicate">查询条件</param>
        /// <returns>实体对象</returns>
        public IQueryable<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate)
        {
            return _dbSet.Where(predicate).Take(1);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        /// <returns>记录总数</returns>
        public IQueryable<int> CountAsync()
        {
            return new[] { _dbSet.Count() }.AsQueryable();
        }

        /// <summary>
        /// 获取符合条件的记录数
        /// </summary>
        /// <param name="predicate">查询条件</param>
        /// <returns>记录数</returns>
        public IQueryable<int> CountAsync(Expression<Func<T, bool>> predicate)
        {
            return new[] { _dbSet.Count(predicate) }.AsQueryable();
        }

        #endregion

        #region 添加操作

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>受影响的行数</returns>
        public async Task<int> AddAsync(T entity)
        {
            await _dbSet.AddAsync(entity);
            return await _dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 批量添加实体
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <returns>受影响的行数</returns>
        public async Task<int> AddRangeAsync(List<T> entities)
        {
            await _dbSet.AddRangeAsync(entities);
            return await _dbContext.SaveChangesAsync();
        }

        #endregion

        #region 更新操作

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>受影响的行数</returns>
        public async Task<int> UpdateAsync(T entity)
        {
            _dbContext.Entry(entity).State = EntityState.Modified;
            return await _dbContext.SaveChangesAsync();
        }
        
        /// <summary>
        /// 更新实体（同步方法）
        /// </summary>
        /// <param name="entity">实体对象</param>
        public void Update(T entity)
        {
            _dbContext.Entry(entity).State = EntityState.Modified;
        }
        
        /// <summary>
        /// 保存所有更改
        /// </summary>
        /// <returns>受影响的行数</returns>
        public async Task<int> SaveAsync()
        {
            return await _dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 批量更新实体
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <returns>受影响的行数</returns>
        public async Task<int> UpdateRangeAsync(List<T> entities)
        {
            foreach (var entity in entities)
            {
                _dbContext.Entry(entity).State = EntityState.Modified;
            }
            return await _dbContext.SaveChangesAsync();
        }

        #endregion

        #region 删除操作

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>受影响的行数</returns>
        public async Task<int> DeleteAsync(T entity)
        {
            _dbSet.Remove(entity);
            return await _dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 批量删除实体
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <returns>受影响的行数</returns>
        public async Task<int> DeleteRangeAsync(IEnumerable<T> entities)
        {
            _dbSet.RemoveRange(entities);
            return await _dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 根据ID删除实体
        /// </summary>
        /// <param name="id">实体ID</param>
        /// <returns>受影响的行数</returns>
        public async Task<int> DeleteByIdAsync(object id)
        {
            var entity = await _dbSet.FindAsync(id);
            if (entity == null)
                return 0;

            _dbSet.Remove(entity);
            return await _dbContext.SaveChangesAsync();
        }

        public Task FindAsync(Func<object, bool> value)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}